﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

using Mighty.Lib.Controls;
using Mighty.Lib.Document;

namespace Mighty
{
    public partial class MightyMainWindow
    {

        private void DefaultFileDoSave(string path, DefaultDocumentTabPage control)
        {
            control.Sync();
            Dictionary<string, object> saveParams = new Dictionary<string, object>();
            saveParams.Add("SaveFilePath", (object)path);
            saveParams.Add("DocumentContents", (object)control.Document.Contents);
            _documentRepo.Save(saveParams);

            FileInfo file = new FileInfo(path);
            control.Document.Name = file.Name;
            control.Document.FullPath = file.FullName;
            control.Document.HasBeenSavedBefore = true;

            control.Document.ParseType = file.Extension;

            // this needs to happen last, since it depends on knowing the (possibly newly changed)
            // name of the document in question
            control.HasBeenSavedSinceLastChange = true;
        }


        private void SaveAsButton_Click(object sender, EventArgs e)
        {
            MightyTabPage page = (MightyTabPage)EditorTabControl.SelectedTab;
            if (!page.IsSavable)
            {
                MessageBox.Show("This is not a savable tab, sorry.");
            }
            else
            {
                // pretty sloppy
                DefaultDocumentTabPage anotherPage = (DefaultDocumentTabPage)page;
                // need to sync

                mainWindowSaveFileDialog.FileName = anotherPage.Document.Name;
                mainWindowSaveFileDialog.OverwritePrompt = false;
                DialogResult saveDialogResult = mainWindowSaveFileDialog.ShowDialog();

                if (saveDialogResult == DialogResult.Cancel) return;

                // checking for overwrite?
                if (File.Exists(mainWindowSaveFileDialog.FileName))
                {
                    DialogResult result = MessageBox.Show("The file " + mainWindowSaveFileDialog.FileName + " already exists. Are you sure want to overwrite it?",
                        "Overwrite existing file?", MessageBoxButtons.YesNo);

                    switch (result)
                    {
                        case DialogResult.Yes:
                            DefaultFileDoSave(mainWindowSaveFileDialog.FileName, anotherPage);
                            break;
                        case DialogResult.No:
                            break;
                    }


                }
                else
                {
                    DefaultFileDoSave(mainWindowSaveFileDialog.FileName,anotherPage);
                }



            }
        }

        private void SaveButton_Click(object sender, EventArgs e)
        {
            MightyTabPage page = (MightyTabPage)EditorTabControl.SelectedTab;
            if (!page.IsSavable)
            {
                MessageBox.Show("This is not a savable tab, sorry.");
            }
            else
            {
                // pretty sloppy
                DefaultDocumentTabPage anotherPage = (DefaultDocumentTabPage)page;
                if (!anotherPage.Document.HasBeenSavedBefore) SaveAsButton_Click(sender, e);
                else
                {
                    DefaultFileDoSave(anotherPage.Document.FullPath, anotherPage);
                }
                    
            }
        }

    }
}
